package com.wantup.miaosha.controller;

import com.alibaba.fastjson.JSONObject;
import com.wantup.miaosha.entity.TblUser;
import com.wantup.miaosha.entity.TblUser2;
import com.wantup.miaosha.pojo.RespInfo;
import com.wantup.miaosha.pojo.UserPojo;
import com.wantup.miaosha.service.UserService;
import com.wantup.miaosha.utils.FastDFSClient;
import com.wantup.miaosha.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/5/31 17:05
 */
@Controller
@RequestMapping(value = "/test")
@Slf4j
public class TestController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private FastDFSClient fastDFSClient;

    /**
     * 接口分为两种 ：
     * 1. json 数据的接口。
     * 2. 跳页面用的。
     */
    @RequestMapping(value = "home")
    public String test(ModelMap modelMap) {
        modelMap.addAttribute("wc", "我的天呢！");

        return "index";
    }

    @RequestMapping(value = "/test3", method = RequestMethod.GET)
    public String hello() {
        return "success";
    }




    @PostMapping(value = "/getUserInofById")
    @ResponseBody
    public RespInfo getUserInofById(@RequestBody JSONObject jsonObject) {
        RespInfo respInfo = new RespInfo();
        try {
            Integer id = jsonObject.getInteger("id");
            TblUser tblUser = this.userService.getUserInofByIdService(id);
            if (tblUser != null) {
                respInfo.setCode(0);
                respInfo.setMessage("查询成功");
                respInfo.setData(tblUser);
            } else {
                respInfo.setCode(1);
                respInfo.setMessage("查询失败,该用户不存在");
                respInfo.setData("");
            }

        } catch (Exception e) {
            e.printStackTrace();
            respInfo.setCode(1);
            respInfo.setMessage("查询失败");
            respInfo.setData("");
        }

        return respInfo;
    }

    @PostMapping(value = "/getRedisValue")
    @ResponseBody
    public RespInfo getRedisValue(@RequestBody JSONObject jsonObject) {
        log.info("传入的参数 --> " + jsonObject);
        RespInfo respInfo = new RespInfo();
        try {

            String key = jsonObject.getString("key");
            String integer = this.redisUtils.get(key, String.class);
            log.info("获取redis中的值 --> " + integer);
            if (integer != null) {
                respInfo.setCode(0).setMessage("获取成功").setData(integer);
            } else {
                respInfo.setCode(1).setMessage("获取失败").setData("");
            }


        } catch (Exception e) {
            e.printStackTrace();
            respInfo.setCode(1).setMessage("获取失败").setData("");
        }

        return respInfo;
    }

    @RequestMapping(value = "/setRedis")
    @ResponseBody
    public RespInfo setRedis(@RequestBody TblUser tblUser) {
        log.info("传入参数 --> " + tblUser);
        RespInfo respInfo = new RespInfo();
        try {
            boolean set = this.redisUtils.set(String.valueOf(tblUser.getId()), tblUser);
            if (set) {
                respInfo.setCode(0).setMessage("存储成功").setData("");
            } else {
                respInfo.setCode(1).setMessage("存储失败").setData("");
            }

        } catch (Exception e) {
            e.printStackTrace();
            respInfo.setCode(-1).setMessage("存储异常").setData("");
        }

        return respInfo;
    }


    @PostMapping(value = "/regUserInfo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public RespInfo regUserInfo(@RequestBody TblUser2 tblUser) {
        log.info(" 用户注册传入参数 --> " + tblUser);
        RespInfo respInfo = new RespInfo();
        try {
            int insert = this.userService.userRegisterService(tblUser);
            if (insert > 0) {
                respInfo.setCode(0).setMessage("插入成功").setData("");
            } else if (insert == 0) {
                respInfo.setCode(1).setMessage("插入失败,该用户已存在").setData("");
            }else {
                respInfo.setCode(1).setMessage("插入失败").setData("");
            }
        } catch (Exception e) {
            e.printStackTrace();
            respInfo.setCode(-1).setMessage("插入异常").setData("");
        }

        return respInfo;
    }



    @PostMapping(value = "/testLogin",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public RespInfo testLogin(@RequestBody UserPojo userPojo){
        log.info("登陆接口 --- 进入 --- ");
        log.info("用户登陆,传入参数 --> "+userPojo);
        RespInfo respInfo = new RespInfo();
        try {
            TblUser tblUser = this.userService.userLoginService(userPojo);
            if (tblUser!=null){
                respInfo.setCode(0).setMessage("登陆成功").setData("");
            }else{
                respInfo.setCode(1).setMessage("登陆失败").setData("");
            }

        }catch (Exception e){
            e.printStackTrace();
            respInfo.setCode(-1).setMessage("登陆异常").setData("");
        }
        log.info("登陆接口 --- 离开 --- ");
        return respInfo;
    }


    /**
     * 文件上传 图片 视频 音频
     *
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public String fileUpload(@RequestParam("pic") MultipartFile file, HttpServletRequest request) {
        if (file == null) {
            return "";
        }

        String url = null;
        try {
            url = fastDFSClient.uploadFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(url);
        return "url  --> "+url;
    }

    /**
     * 多文件 上传
     *
     * @param files
     * @param request
     * @return
     */
    @RequestMapping("/moreUpload")
    @ResponseBody
    public String moreFileUpload(@RequestParam(value = "files") MultipartFile files[], HttpServletRequest request) {
        System.out.println(files.length);
        String url = "";
        for (int i = 0; i < files.length; i++) {
            try {
                String uploadFile = fastDFSClient.uploadFile(files[i]);
                url += uploadFile + "\n";
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(url);
        return url;
    }


    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String deleteFile(String fileUrl) {
        System.out.println(fileUrl);

        Integer integer = fastDFSClient.deleteFile2(fileUrl);
        if (integer == 0) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }




}
